package com.example.dworld3;

import java.util.ArrayList;

import org.Consts;
import org.Globals;
import org.sqlStatements;

import com.ydev.dworld3.R;

import CustomUI.AmbilWarnaDialog;
import CustomUI.AmbilWarnaDialog.OnAmbilWarnaListener;
import CustomUI.BrushPropertiesDialog;
import CustomUI.DrawingSurface;
import CustomUI.Grid_Properties_Dialog;
import CustomUI.LayerView;
import CustomUI.ShapePropertiesDialog;
import CustomUI.TouchView;
import CustomUI.ydButton;
import CustomUI.ydCustomColorPicker;
import CustomUI.ydDrawingCell;
import CustomUI.ydImageCell;
import CustomUI.ydLayerCell;
import CustomUI.ydLayerHolder;
import CustomUI.ydScrollView;
import CustomUI.ydSeekBar;
import DTO.DrawingCmdDTO;
import DTO.DrawingDTO;
import DTO.LayerDTO;
import DTO.NextStep;
import DTO.Properties;
import DTO.PropertiesDTO;
import GameTools.ydPoint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Path;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class Draw extends Activity {

	DrawingDTO drawing;

	int currentLayer = 0;
	String currentAction = "Brush";
	int currentInstruct = 0;
	
	int controls=1;

	DrawingSurface drawingSurface;
	TouchView touchView;

	RelativeLayout rrtouchControls;
	LinearLayout llTransformRadioHolder;
	ydSeekBar ydSeekBarH, ydSeekBarV, ydSeekBarC;
	ydButton ydButtonCheck, ydButtonCancel;
	RadioButton rbMove, rbScale, rbRotate;

	public LinearLayout linearLayoutDrawingTools;
	public LinearLayout linearLayoutDrawingToolShapes;
	public LinearLayout linearLayoutArrows;

	ydButton ydButtonShowDrawingTools, ydButtonShowLayers, ydButtonBrush,
			ydButtonErase, ydButtonShape, ydButtonText, ydButtonColor,
			ydButtonUndo, ydButtonRedo, ydButtonBack;

	ydScrollView sv;

	ydButton ydbuttonSelect, ydButtonMove, ydButtonRotate, ydButtonScale;

	ydButton ydButtonSave;

	// Properties brushProp = new Properties(Consts.DRAWING_ACTIONS_BRUSH);
	// Properties shapeProp = new Properties(Consts.DRAWING_ACTIONS_Shape);

	Properties currentProperties = new Properties();

	ydCustomColorPicker ydCCP;
	AmbilWarnaDialog dialog;

	LinearLayout linearLayoutLayers;
	ydLayerHolder linearLayoutLayerHolder;
	Button btnNewLayer, btnDeleteLayer;

	Draw MySelf = this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Remove title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		// Remove notification bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.draw);

		init();
	}

	private void init() {

		DrawingCmdDTO.mContext = getApplicationContext();
		LayerDTO.mContext = getApplicationContext();

		ydCCP = new ydCustomColorPicker(this);

		drawingSurface = (DrawingSurface) findViewById(R.id.drawingSurface1);
		LayerView.drawingSurface = drawingSurface;

		linearLayoutDrawingToolShapes = (LinearLayout) findViewById(R.id.linearLayoutDrawingToolShapes);

		touchView = (TouchView) findViewById(R.id.touchView1);
		rrtouchControls = (RelativeLayout) findViewById(R.id.rrtouchControls);
		llTransformRadioHolder=(LinearLayout) findViewById(R.id.llTransformRadioHolder);

		rbMove = (RadioButton) findViewById(R.id.rbMove);
		rbScale = (RadioButton) findViewById(R.id.rbScale);
		rbRotate = (RadioButton) findViewById(R.id.rbRotate);

		linearLayoutLayers = (LinearLayout) findViewById(R.id.linearLayoutLayers);
		linearLayoutLayerHolder = (ydLayerHolder) findViewById(R.id.linearLayoutLayerHolder);
		btnNewLayer = (Button) findViewById(R.id.ButtonNewLayer);
		btnDeleteLayer=(Button) findViewById(R.id.buttonDeleteLayer);

		currentProperties.setColor(Color.BLACK);
		currentProperties.setStrokeSize(3);
		currentProperties.setFill(0);

		touchView.setProperties(currentProperties);

		Bundle extras = getIntent().getExtras();
		if (extras.containsKey(Consts.DTO_PK)) {
			int object_id = extras.getInt(Consts.DTO_PK);

			if (object_id != -1) {
				drawing = new DrawingDTO(Globals.sqlDB, object_id);
				currentInstruct = drawing.lastState;
				currentLayer = drawing.getFirstLayerID();
				drawing.draw(drawingSurface);
				insertAllLayersUpToo(drawing.lastState);
				layerCellClicked(drawing.getFirstLayerID());
			} else {

				drawingSurface.post(new Runnable() {
					@Override
					public void run() {
						drawing = new DrawingDTO(Globals.sqlDB);
						drawing.addLayer(drawingSurface, currentInstruct);
						currentLayer = drawing.getFirstLayerID();
						drawing.getLayer(currentLayer).getLV()
								.post(new Runnable() {

									@Override
									public void run() {
										drawing.getLayer(currentLayer)
												.CreateFirstCMD(currentInstruct);
										currentInstruct++;
										insertLayers();
										layerCellClicked(drawing
												.getFirstLayerID());

									}
								});
					}
				});

				/*
				 * new Thread(new Runnable() {
				 * 
				 * @Override public void run() { drawing = new
				 * DrawingDTO(Globals.sqlDB); runOnUiThread(new Runnable() {
				 * 
				 * @Override public void run() {
				 * 
				 * drawing.addLayer(drawingSurface, currentInstruct);
				 * 
				 * } }); currentLayer = drawing.getFirstLayerID();
				 * drawing.getLayer
				 * (currentLayer).CreateFirstCMD(currentInstruct);
				 * currentInstruct++; insertLayers();
				 * layerCellClicked(drawing.getFirstLayerID()); } }).run();
				 */

				// drawing.drawAll(drawingSurface);
			}
		}

		btnNewLayer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				currentLayer = drawing
						.addLayer(drawingSurface, currentInstruct);
				drawing.getLayer(currentLayer).getLV().post(new Runnable() {

					@Override
					public void run() {
						drawing.getLayer(currentLayer).CreateFirstCMD(
								currentInstruct);
						currentInstruct++;
						insertLayers();
						layerCellClicked(currentLayer);

					}
				});
				/*
				 * currentLayer = drawing .addLayer(drawingSurface,
				 * currentInstruct); currentInstruct++;
				 */
			}
		});

		btnDeleteLayer.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int cl=currentLayer;
				removeLayer(currentLayer);
				LayerView.drawingSurface.removeLayerView(drawing.getLayer(cl).getLV());
				
				ArrayList<ydPoint> coordinates;
				coordinates = new ArrayList<ydPoint>();
				coordinates.add(new ydPoint());

				DrawingCmdDTO DC = new DrawingCmdDTO();
				DC.setCommand(sqlStatements.DC_DELETE_LAYER);
				DC.setCoordinates(coordinates);
				DC.setInstructNum(currentInstruct);
				DC.setLid(cl);
				DC.setImage(drawing.getLayer(cl).getImage());
				drawing.addToLayer(cl, DC);
				currentInstruct++;
				drawing.setLastState(currentInstruct);
				new MyAsyncTask().execute("btnDeleteLayer", drawing, MySelf, -1);
				
			}
		});
		
		ydbuttonSelect=(ydButton) findViewById(R.id.ydButtonSelect);
		ydbuttonSelect.addOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				transformLayerControls();
				
				
			}
		});
		
		ydSeekBarH = (ydSeekBar) findViewById(R.id.ydSeekBarHorizontal);
		ydSeekBarV = (ydSeekBar) findViewById(R.id.ydSeekBarVertical);
		ydSeekBarC = (ydSeekBar) findViewById(R.id.ydSeekBarCircular);

		ydSeekBarC.setVisibility(View.GONE);

		rbMove.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					rrtouchControls.bringToFront();
					ydSeekBarH.setVisibility(View.VISIBLE);
					ydSeekBarV.setVisibility(View.VISIBLE);
					ydSeekBarC.setVisibility(View.GONE);

				}

			}
		});

		rbScale.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					rrtouchControls.bringToFront();
					ydSeekBarH.setVisibility(View.VISIBLE);
					ydSeekBarV.setVisibility(View.VISIBLE);
					ydSeekBarC.setVisibility(View.GONE);
				}

			}
		});

		rbRotate.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					rrtouchControls.bringToFront();
					ydSeekBarH.setVisibility(View.GONE);
					ydSeekBarV.setVisibility(View.GONE);
					ydSeekBarC.setVisibility(View.VISIBLE);

					ydSeekBarC.post(new Runnable() {

						@Override
						public void run() {
							ydSeekBarC.resetOrigin();
							ydSeekBarC.invalidate();

						}
					});

				}

			}
		});

		sv = (ydScrollView) findViewById(R.id.ydScrollView1);
		sv.init();

		ydButtonShowDrawingTools = (ydButton) findViewById(R.id.ydButtonShowDrawingTools);
		ydButtonShowDrawingTools
				.addOnLongClickListener(new View.OnLongClickListener() {

					@Override
					public boolean onLongClick(View v) {
						Toast.makeText(getApplicationContext(), "msg msg",
								Toast.LENGTH_LONG).show();
						return true;
					}

				});

		ydButtonBack = (ydButton) findViewById(R.id.ydButtonBack);
		showHideDrawingTools(ydButtonBack);

		ydButtonShowLayers = (ydButton) findViewById(R.id.ydButtonShowLayers);
		ydButtonShowLayers
				.addOnLongClickListener(new View.OnLongClickListener() {

					@Override
					public boolean onLongClick(View v) {
						Toast.makeText(getApplicationContext(),
								"ydButtonShowLayers", Toast.LENGTH_LONG).show();
						return true;
					}

				});

		ydButtonCheck = (ydButton) findViewById(R.id.ydButtonCheck);
		ydButtonCheck.addOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(controls==1){
					touchView.ConfirmClicked();
				}
				else if(controls==2){
					LayerDTO l=drawing.getLayer(currentLayer);
					
					ArrayList<ydPoint> coordinates;
					coordinates = new ArrayList<ydPoint>();
					coordinates.add(new ydPoint());
					
					DrawingCmdDTO DC = new DrawingCmdDTO();
					DC.setCommand(sqlStatements.TRANSFORM_LAYER);
					DC.setCoordinates(coordinates);
					DC.setInstructNum(currentInstruct);
					DC.setLid(currentLayer);
					DC.setInstructNum(currentInstruct);

					DC.setProperties(currentProperties);
					
					DC.setImage(l.getLV().getImage());

					drawing.addToLayer(currentLayer, DC, false);

					currentInstruct++;
					drawing.setLastState(currentInstruct);
					new MyAsyncTask().execute("addCMD", drawing, MySelf);
					insertLayers();
					l.ConfirmClicked();
					l.setImage(DC.getImage());
				}
				
				rrtouchControls.setVisibility(View.GONE);
				sv.setVisibility(View.VISIBLE);

			}
		});

		ydButtonCancel = (ydButton) findViewById(R.id.ydButtonX);
		ydButtonCancel.addOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(controls==1){
					touchView.CancelClicked();
				}
				else if(controls==2){
					LayerDTO l=drawing.getLayer(currentLayer);
					l.CancelClicked();
				}
				
				rrtouchControls.setVisibility(View.GONE);
				sv.setVisibility(View.VISIBLE);
			}
		});

		ydButtonUndo = (ydButton) findViewById(R.id.ydButtonUndo);
		ydButtonUndo.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (currentInstruct > 0) {
					int l = -1;
					NextStep ns;
					ns = drawing.Undo(currentInstruct - 1);
					if (ns != null) {
						currentInstruct--;
						drawing.setLastState(currentInstruct);
						new MyAsyncTask().execute("Undo", drawing, MySelf);
						if (ns.getAction().equals(NextStep.DELETE)) {
							removeLayer(ns.getId());
						} else if (ns.getAction().equals(NextStep.UPDATE)) {
							updateLayer(ns.getId());
						} else if (ns.getAction().equals(NextStep.MOVE_UP)) {
							moveUp(ns.getId(), false);
						} else if (ns.getAction().equals(NextStep.MOVE_DOWN)) {
							moveDown(ns.getId(), false);
						}
						else if(ns.getAction().equals(NextStep.ADD)){
							LayerDTO layer=drawing.getLayer(ns.getId());
							addLayer(ns.getId(), layer.getZindex());
						}
					}
				}
			}
		});

		ydButtonRedo = (ydButton) findViewById(R.id.ydButtonRedo);
		ydButtonRedo.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int l = -1;
				NextStep ns;
				ns = drawing.Redo(currentInstruct);
				if (ns != null) {
					currentInstruct++;
					drawing.setLastState(currentInstruct);
					new MyAsyncTask().execute("Redo", drawing, MySelf);
					if (ns.getAction().equals(NextStep.ADD)) {
						addLayer(ns.getId());
					} else if (ns.getAction().equals(NextStep.UPDATE)) {
						updateLayer(ns.getId());
					} else if (ns.getAction().equals(NextStep.MOVE_UP)) {
						moveUp(ns.getId(), false);
					} else if (ns.getAction().equals(NextStep.MOVE_DOWN)) {
						moveDown(ns.getId(), false);
					} else if (ns.getAction().equals(NextStep.DELETE)) {
						removeLayer(ns.getId());
					} 
				}

			}
		});

		dialog = new AmbilWarnaDialog(this, Color.RED,
				new OnAmbilWarnaListener() {

					@Override
					public void onOk(AmbilWarnaDialog dialog, int color) {
						currentProperties.setColor(color);
						touchView.setProperties(currentProperties);

					}

					@Override
					public void onCancel(AmbilWarnaDialog dialog) {
						// TODO Auto-generated method stub

					}
				});

		ydButtonColor = (ydButton) findViewById(R.id.ydButtonColor);
		ydButtonColor.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.show();
			}
		});

		ydButtonBrush = (ydButton) findViewById(R.id.ydButtonBrush);

		final BrushPropertiesDialog BPD = new BrushPropertiesDialog(this);
		ydButtonBrush.addOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {

				BPD.show();
				return true;
			}

		});

		ydButtonShape = (ydButton) findViewById(R.id.ydButtonShape);
		ydButtonShape.addOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				/*
				 * Toast.makeText(getApplicationContext(), "msg msg",
				 * Toast.LENGTH_LONG).show();
				 */

				// ydButtonShape.setSelected(true);

				// btn.setSelected(selected);
				// linearLayoutDrawingToolShapes.setVisibility(View.VISIBLE);
				return true;
			}

		});

		final ShapePropertiesDialog SPD = new ShapePropertiesDialog(this);
		ydButtonShape.addOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {

				SPD.show();
				return true;
			}

		});

		final Grid_Properties_Dialog GPD = new Grid_Properties_Dialog(this);
		ydButtonText = (ydButton) findViewById(R.id.ydButtonText);
		ydButtonText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				GPD.show();
			}
		});
		
		ydButtonErase=(ydButton) findViewById(R.id.ydButtonErase);
		ydButtonErase.addOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Properties p=touchView.getProperties();
				 p.setAction(sqlStatements.DC_ERASE);
				 p.setColor(Color.TRANSPARENT);
				 touchView.setProperties(p);
				
			}
		});

		showHideDrawingTools(ydButtonShowDrawingTools);
		showHideLayers(ydButtonShowLayers);

	}

	private void insertAllLayersUpToo(int lastState) {

		ArrayList<LayerDTO> layers = drawing.getLayer();
		LayerDTO l;
		ydLayerCell im2;
		for (int i = 0; i < layers.size(); i++) {
			l = layers.get(i);
			DrawingCmdDTO d = l.getDrawingsCmds().get(0);
			if (d.instructNum >= lastState)
				continue;
			im2 = new ydLayerCell(this);
			im2.setLayerID(l.getLid());
			im2.setImage(l.getImage());
			im2.setLayerView(l.getLV());
			im2.llMain.setBackgroundColor(Color.WHITE);
			linearLayoutLayerHolder.addLayerCell(im2);
		}

	}

	protected void updateLayer(int id) {
		int prev = -1;
		ydLayerCell im;
		for (int i = 0; i < linearLayoutLayerHolder.getChildCount(); i++) {
			im = (ydLayerCell) linearLayoutLayerHolder.getChildAt(i);
			if (im.getlID() == id) {
				im.setImage(drawing.getLayer(id).getImage(currentInstruct));
			}
		}

	}

	protected void addLayer(int l) {
		if (l == -1)
			return;

		final ydLayerCell im2;
		im2 = new ydLayerCell(this);
		im2.setLayerID(l);
		im2.setImage(drawing.getLayer(l).getImage(currentInstruct));
		linearLayoutLayerHolder.addLayerCell(im2);

	}

	protected void addLayer(int l, int zindex) {
		if (l == -1)
			return;
		/*if(drawing.getLayer().size()==0){
			addLayer(l);
			return;
		}*/

		final ydLayerCell im2;
		im2 = new ydLayerCell(this);
		im2.setLayerID(l);
		im2.setImage(drawing.getLayer(l).getImageBefore(currentInstruct));
		linearLayoutLayerHolder.addLayerCell(im2,zindex,drawing);
		
	}

	private void insertLayers() {

		if (linearLayoutLayerHolder.getChildCount() == 0) {
			final ydLayerCell im = new ydLayerCell(this);
			im.setLayerID(currentLayer);
			im.setImage(drawing.getLayer().get(0).getImage(currentInstruct));
			im.llMain.setBackgroundColor(Color.BLUE);
			im.setLayerView(drawing.getLayer().get(0).getLV());
			linearLayoutLayerHolder.addLayerCell(im);
		}

		else {
			ydLayerCell im;
			for (int i = 0; i < linearLayoutLayerHolder.getChildCount(); i++) {
				im = (ydLayerCell) linearLayoutLayerHolder.getChildAt(i);
				if (im.getlID() == currentLayer) {
					im.setImage(drawing.getLayer(currentLayer).getImage());
					return;
				}
			}
			LayerDTO l = drawing.getLayer(currentLayer);
			final ydLayerCell im2;
			im2 = new ydLayerCell(this);
			im2.setLayerID(currentLayer);
			im2.setImage(l.getImage());
			im2.setLayerView(l.getLV());
			im2.llMain.setBackgroundColor(Color.WHITE);
			linearLayoutLayerHolder.addLayerCell(im2);
		}

	}

	protected void removeLayer(int l) {
		if (l == -1)
			return;

		int prev = -1;
		ydLayerCell im;
		for (int i = 0; i < linearLayoutLayerHolder.getChildCount(); i++) {
			im = (ydLayerCell) linearLayoutLayerHolder.getChildAt(i);
			if (im.getlID() == l) {
				linearLayoutLayerHolder.removeLayerCell(im);
				if (prev != -1) {
					layerCellClicked(prev);
				}
			}
			prev = im.getlID();
		}

	}

	public void layerCellClicked(int id) {
		ydLayerCell im;
		for (int i = 0; i < linearLayoutLayerHolder.getChildCount(); i++) {
			im = (ydLayerCell) linearLayoutLayerHolder.getChildAt(i);
			if (im.getlID() == id) {
				im.llMain.setBackgroundColor(Color.BLUE);
			} else {
				im.llMain.setBackgroundColor(Color.WHITE);
			}
		}
		currentLayer = id;
	}

	private void showHideLayers(ydButton btn) {
		btn.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (linearLayoutLayers.getVisibility() == View.VISIBLE) {
					linearLayoutLayers.setVisibility(View.GONE);
				} else {
					linearLayoutLayers.setVisibility(View.VISIBLE);
				}
			}
		});

	}

	public void showHideDrawingTools(ydButton btn) {
		btn.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (sv.getVisibility() == View.VISIBLE) {
					linearLayoutDrawingToolShapes.setVisibility(View.GONE);
					sv.setVisibility(View.GONE);
					ydButtonShowDrawingTools.setVisibility(View.VISIBLE);
				} else {
					sv.setVisibility(View.VISIBLE);
					ydButtonShowDrawingTools.setVisibility(View.GONE);
				}
			}
		});
	}

	public void touchedEnded() {

		rrtouchControls.setVisibility(View.VISIBLE);
		showHideTransformationTools(View.VISIBLE);

		rbMove.setChecked(true);
		rbRotate.setChecked(false);
		rbScale.setChecked(false);

		rrtouchControls.post(new Runnable() {

			@Override
			public void run() {

				rrtouchControls.bringToFront();

				ydSeekBarH.resetOrigin();
				ydSeekBarV.resetOrigin();
				ydSeekBarC.resetOrigin();

				ydSeekBarH.invalidate();
				ydSeekBarV.invalidate();
				ydSeekBarC.invalidate();

			}
		});

	}

	public void ColorSelected(int i) {
		// touchView.setSelectedColor(i);
		touchView.setProperties(currentProperties);

	}

	public void setPathProperties(int brushWidth, int fillProperty) {
		currentProperties.setStrokeSize(brushWidth);
		currentProperties.setFill(fillProperty);
		touchView.setProperties(currentProperties);
		// touchView.setPathProperties(brushWidth, fillProperty);

	}

	public void setShapePathProperties(Properties props) {
		currentProperties.setStrokeSize(props.getStrokeSize());
		currentProperties.setFill(props.getFill());
		currentProperties.setShape(props.getShape());
		currentProperties.setAction(props.getAction());
		currentProperties.setSym(props.getSym());
		touchView.setProperties(currentProperties);

	}

	public void moveUp(int id, Boolean save) {
		if (drawingSurface.moveUp(id)) {
			linearLayoutLayerHolder.moveUp(id);
			if (save) {
				ArrayList<ydPoint> coordinates;
				coordinates = new ArrayList<ydPoint>();
				coordinates.add(new ydPoint());

				DrawingCmdDTO DC = new DrawingCmdDTO();
				DC.setCommand(sqlStatements.DC_MOVE_UP);
				DC.setCoordinates(coordinates);
				DC.setInstructNum(currentInstruct);
				DC.setLid(currentLayer);
				drawing.addToLayer(currentLayer, DC);
				currentInstruct++;
				drawing.setLastState(currentInstruct);
				new MyAsyncTask().execute("moveUp", drawing, MySelf, id);
			}

		}
	}

	public void moveDown(int id, Boolean save) {
		if (drawingSurface.moveDown(id)) {
			linearLayoutLayerHolder.moveDown(id);

			if (save) {
				ArrayList<ydPoint> coordinates;
				coordinates = new ArrayList<ydPoint>();
				coordinates.add(new ydPoint());

				DrawingCmdDTO DC = new DrawingCmdDTO();
				DC.setCommand(sqlStatements.DC_MOVE_DOWN);
				DC.setInstructNum(currentInstruct);
				DC.setCoordinates(coordinates);
				DC.setLid(currentLayer);
				drawing.addToLayer(currentLayer, DC);
				currentInstruct++;
				drawing.setLastState(currentInstruct);
				new MyAsyncTask().execute("moveDown", drawing, MySelf, id);
			}
		}

	}

	public void lastStateUpdated(int x, final int lastState) {

		new Thread(new Runnable() {

			@Override
			public void run() {

				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						Toast.makeText(MySelf.getApplicationContext(),
								"Saved Last State As " + lastState + "",
								Toast.LENGTH_LONG).show();
					}
				});

			}
		}).run();

	}

	public void touchedStarted() {

		sv.setVisibility(View.GONE);
		ydButtonShowDrawingTools.setVisibility(View.GONE);

	}

	public void addCMD(Path currentPath) {
		DrawingCmdDTO DC = new DrawingCmdDTO();
		DC.setCommand(currentAction);
		DC.setCoordinates(ydPoint.PathToArray(currentPath));
		DC.setInstructNum(currentInstruct);
		DC.setLid(currentLayer);
		DC.setInstructNum(currentInstruct);

		DC.setProperties(currentProperties);

		drawing.addToLayer(currentLayer, DC);

		currentInstruct++;
		drawing.setLastState(currentInstruct);
		new MyAsyncTask().execute("addCMD", drawing, MySelf);
		insertLayers();

	}

	public void movePath(int type, int from, int to) {
		int t = -1;
		if (rbMove.isChecked()) {
			t = 1;
		} else if (rbScale.isChecked()) {
			t = 2;
		} else if (rbRotate.isChecked()) {
			t = 3;
		}
		touchView.movePath(type, t, from, to);

	}

	public void transformLayer(int type, int from, int to) {
		int t = -1;
		if (rbMove.isChecked()) {
			t = 1;
		} else if (rbScale.isChecked()) {
			t = 2;
		} else if (rbRotate.isChecked()) {
			t = 3;
		}
		LayerDTO l=drawing.getLayer(currentLayer);
		l.transform(type, t, from, to);
		
	}

	public void setGridProperties(int h, int v, boolean on, boolean isSym) {
		touchView.setGridProperties(h, v, on, isSym);

	}

	protected void transformLayerControls() {
		
		controls=2;
		
		ydSeekBarV.setsID(controls);
		ydSeekBarC.setsID(controls);
		ydSeekBarH.setsID(controls);
		
		touchedStarted();
		touchedEnded();
		
	}

	public void transformTouchViewControls() {
		
		controls=1;
		
		ydSeekBarV.setsID(controls);
		ydSeekBarC.setsID(controls);
		ydSeekBarH.setsID(controls);
		
	}

	public void erase(Path currentPath) {
		LayerDTO l=drawing.getLayer(currentLayer);
		l.erase(currentPath);
		
	}

	public void showHideTransformationTools(int i) {
		ydSeekBarV.setVisibility(i);
		//ydSeekBarC.setVisibility(i);
		//ydSeekBarH.setVisibility(i);
		
		llTransformRadioHolder.setVisibility(i);
		
	}
	

}

class MyAsyncTask extends AsyncTask<Object, Void, Boolean> {
	@Override
	protected Boolean doInBackground(Object... objs) {
		String cmd = (String) objs[0];
		if (cmd.equals("moveUp")) {
			int id = (Integer) objs[3];
			LayerDTO l = ((DrawingDTO) objs[1]).getLayer(id);
			LayerDTO l2 = ((DrawingDTO) objs[1]).getLayerWithZ(l.zindex - 1);
			l2.setZindex(l.zindex);
			l.setZindex(l.zindex - 1);
			l.updateZIndex();
			l2.updateZIndex();
		} else if (cmd.equals("moveDown")) {
			int id = (Integer) objs[3];
			LayerDTO l = ((DrawingDTO) objs[1]).getLayer(id);
			LayerDTO l2 = ((DrawingDTO) objs[1]).getLayerWithZ(l.zindex + 1);
			l2.setZindex(l.zindex);
			l.setZindex(l.zindex + 1);
			l.updateZIndex();
			l2.updateZIndex();
		} else {
			int x = ((DrawingDTO) objs[1]).update();
			((Draw) objs[2]).lastStateUpdated(x,
					((DrawingDTO) objs[1]).getLastState());
		}

		return null;

	}

	protected void onPostExecute(Double result) {

	}

	protected void onProgressUpdate(Integer... progress) {

	}
}
